//
//  OrderList.m
//  Recipe
//
//  Created by bing han on 12-7-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "OrderList.h"

@implementation OrderList
static OrderList *instence = nil;

@synthesize disherOrder;

+(OrderList *) newInstance{
    @synchronized(self){
        if (instence == nil) {
            instence = [[OrderList alloc] init];
            instence.disherOrder = [[NSMutableDictionary alloc] init];
        }
    }
    return  instence;
}

+(id) allocWithZone:(NSZone *)zone{
    @synchronized(self){
        if (instence == nil) {
            instence = [super allocWithZone:zone];
            return instence;
        }
    }
    return nil;
}

-(id) copyWithZone:(NSZone *)zone{
    return self;
}

-(id) retain{
    return self;
}

-(unsigned) retainCount{
    return UINT_MAX;
}

-(id) autorelease{
    return self;
}

-(void) addDisher:(NSString*)key{
    if ([instence.disherOrder objectForKey:key] ==nil) {
        NSInteger countint = [@"1" intValue];
        NSString *countstr = [NSString stringWithFormat:@"%d",countint];
        [instence.disherOrder setObject:countstr forKey:key];
    }else {
        NSString *countstr = [instence.disherOrder objectForKey:key];
        NSInteger countint = [countstr intValue];
        countint += 1;
        NSString *count = [NSString stringWithFormat:@"%d",countint];
        [instence.disherOrder setObject:count forKey:key];
    }    
}

-(void) substractDisher:(NSString*)key{
    if ([instence.disherOrder objectForKey:key] ==nil) {
        NSInteger countint = [@"0" intValue];
        NSString *countstr = [NSString stringWithFormat:@"%d",countint];
        [instence.disherOrder setObject:countstr forKey:key];
    }else {
        NSString *countstr = [instence.disherOrder objectForKey:key];
        NSInteger countint = [countstr intValue];
        countint -= 1;
        if (countint <= 0) {
            countint = 0;
            [instence.disherOrder removeObjectForKey:key];
        }else {
            NSString *count = [NSString stringWithFormat:@"%d",countint];
            [instence.disherOrder setObject:count forKey:key];

        }
    }    
}


@end
